<?php

namespace App\Http\Controllers\Seller;

use App\Http\Controllers\Controller;
use App\Qingwuit\Models\Address;
use App\Qingwuit\Models\Order;
use App\Qingwuit\Models\OrderGoods;
use App\Qingwuit\Models\Parlours;
use App\Qingwuit\Models\Riders;
use App\Qingwuit\Models\Store;
use Illuminate\Http\Request;

class OrdersController extends Controller
{
    protected $modelName = 'Order';
    protected $auth = 'users';

    public function index(Request $request)
    {
//        dd(1);
        $data = $this->getService('base')->getPageData($this->modelName, ['store_id' => $this->getService('Store')->getStoreId()['data']]);
        return $this->handle($data);
    }

    // 获取订单商品格式化列表
    public function all()
    {
        return $this->handle($this->getService('Order')->createOrderAfter());
    }

    // 编辑发货信息
    public function edit(Request $request)
    {
        $id = $request->id ?? 0;
        $status = $request->status ?? 3;
        return $this->handle($this->getService('Order')->editOrderStatus($id, $status, 'seller'));
    }

    // 修改订单信息
    public function update(Request $request, $id)
    {

        return $this->handle($this->getService('base')->editDat($this->modelName, $id, ['delivery_no', 'delivery_code', 'receive_name', 'receive_tel', 'receive_area', 'receive_address']));
    }

    // 物流查询
    public function express(Request $request)
    {

        // 根据订单Id查询
        $order = $this->getService('Order', true)->where(['store_id' => $this->getService('Store')->getStoreId()['data'], 'id' => $request->id])->first();
        if (!$order) return $this->error(__('tip.order.empty'));
        return $this->handle($this->getService('KuaiBao')->getExpressInfo($order->delivery_no, $order->delivery_code, $order->receive_tel));
    }

    // 云打印订单
    public function print_waybill($id)
    {
        $rs = $this->getService('KuaiBao')->printWaybill($id);
        return $this->handle($rs);
    }


    public function orderList(){
        $user_id = $this->getUserId('users');
        if (empty($user_id)) return $this->error('请登录');
        $stores_id = Store::where('user_id',$user_id)->pluck('id');
        if (empty($stores_id)) return $this->error('没有店铺权限');
        $order_status = \request()->order_status;
        $seach_name = \request()->seach_name;
        $data = $this->getService('Order')->orderList($stores_id,$order_status,$seach_name);
        return $this->handle($data);
    }
    public function orderdetails()
    {
        $order_id = \request()->order_id;
        $data = Order::where('id', $order_id)
            ->select('id','order_no','total_price','created_at','order_status','pickup_code','store_id','user_id','riders')
            ->first();
        $data->riders_name = '';
        $data->riders_phone = '';
        if ($data->riders){
            $rider = Riders::where('user_id',$data->riders)->first();
            if ($rider){
                $data->riders_name = $rider->name;
                $data->riders_phone = $rider->phone;
            }
        }
        $par = Address::where('user_id',$data->user_id)->first();
        $p_name = Parlours::where('userid',$data->user_id)->first();
        $data->par_name = $p_name->parlour_name;
        $data->receive_name = $par->receive_name;
        $data->receive_tel = $par->receive_tel;
        $store = Store::where('id',$data->store_id)->first();
        $data->store_address = $par->address;
        if (empty($data)) return $this->error('暂无此订单');
        $data->chinren = OrderGoods::where('order_id',$data->id)->get();
        return $this->success($data);
    }
    //退货
    public function editRefund()
    {
        $order_id = \request()->order_id;
        $user_id = $this->getUserId('users');
        $store_id = Store::where('user_id', $user_id)->value('id');
        $refund_model = $this->getService('Refund', true);

        $refund_info = $refund_model->where('order_id', $order_id)->first();
        if (!$refund_info) {
            return $this->formatError(__('tip.error'));
        }

        if (isset(request()->refund_verify)) {
            $refund_verify = request()->refund_verify;
            if ($refund_info->refund_verify != 0) {
                return $this->formatError(__('tip.error') . '- rep');
            }

            // 拒绝还是同意
            if ($refund_verify == 1) {
                $order_info = $this->getService('Order', true)->where('id', $order_id)->first();
                if (!preg_match("/^\d+$/", \request()->refund_amount)){
                    return $this->error('请输入数字');
                }
                $this->getService('MoneyLog')->edit([
                    'name'=>'售后退款',
                    'money'=>\request()->refund_amount,
                    'user_id'=>$refund_info->user_id,
                    'info'=>$order_info['order_no'],
                ]);
                    $refund_amount = \request()->refund_amount;

                // 修改订单状态
                $order_info = $this->getService('Order', true)->where('id', $order_id)->where('store_id', $store_id)->first();
                if ($refund_amount>$order_info->order_pirce){
                    $this->error('退款金额不能大于订单金额');
                }
                $order_info->order_status = 5;

                $order_info->refund_status = 1;
                $order_info->save();
                $refund_info->refund_step = 3;
                $refund_info->refund_amount = $refund_amount;
                $refund_info->refund_verify = 1;
                $rs = $refund_info->save();
            } else {
                $refund_info->refund_verify = 2;
                $refund_info->refuse_remark = request()->refuse_remark ?? '暂无任何原因';
                // 修改订单状态
                $order_model = $this->getService('Order', true);
                $order_info = $order_model->where('id', $order_id)->where('store_id', $store_id)->first();
                $order_info->order_status = 5;
                $order_info->refund_status = 2;
                $refund_info->refund_verify = 2;
                $order_info->save();

                $rs = $refund_info->save();
            }

//            // 修改订单状态
//            $order_model = $this->getService('Order', true);
//            $order_info = $order_model->where('id', $order_id)->where('user_id', $refund_info->user_id)->first();
//            $order_info->order_status = 6;
//            $order_info->refund_status = 2;
//            $order_info->save();


//
            return $this->format($rs, __('tip.success'));
        }

    }

}
